package com.cosmoplat.xikai.business.service.impl;

import com.cosmoplat.xikai.business.domain.BaseDispatchTask;
import com.cosmoplat.xikai.business.domain.BaseProductInfo;
import com.cosmoplat.xikai.business.domain.dto.quality.QualityHandleDTO;
import com.cosmoplat.xikai.business.domain.dto.quality.QualityQueryDTO;
import com.cosmoplat.xikai.business.domain.vo.ProcessingDataVo;
import com.cosmoplat.xikai.business.domain.vo.ProductQuality;
import com.cosmoplat.xikai.business.service.BaseAlarmRecordService;
import com.cosmoplat.xikai.business.service.BaseDispatchTaskService;
import com.cosmoplat.xikai.business.service.BaseProductInfoService;
import com.cosmoplat.xikai.business.service.QualityService;
import com.cosmoplat.xikai.common.core.domain.model.LoginUser;
import com.cosmoplat.xikai.common.enums.business.AlarmTypeEnum;
import com.cosmoplat.xikai.common.exception.ServiceException;
import com.cosmoplat.xikai.common.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * @ClassName QualityServiceImpl
 * @Description 质量管理实现
 * @Author wdf
 * @Date 2025/1/13
 */
@Slf4j
@Service
public class QualityServiceImpl implements QualityService {

    @Autowired
    private BaseProductInfoService productInfoService;

    @Autowired
    private BaseAlarmRecordService alarmRecordService;

    @Autowired
    private BaseDispatchTaskService dispatchTaskService;


    @Override
    public List<ProcessingDataVo> queryProcessingDataForPage(QualityQueryDTO queryDTO) {
        return productInfoService.queryProcessingDataForPage(queryDTO);
    }

    @Override
    public List<ProductQuality> queryProductQuality(QualityQueryDTO queryDTO) {
        return productInfoService.queryProductQuality(queryDTO);
    }

    @Transactional
    @Override
    public boolean handleProductQuality(QualityHandleDTO handleDTO) {
        BaseProductInfo productInfo = productInfoService.getById(handleDTO.getId());
        if (productInfo == null) {
            throw new ServiceException("产品不存在");
        }
        if (handleDTO.getRemark().length() > 500) {
            throw new ServiceException("处理原因不能超过500个字符");
        }
        // 关闭质量告警
        BaseDispatchTask task = dispatchTaskService.getByTaskId(productInfo.getBaseDispatchTaskId());
        if (task == null) {
            log.info("该产品没有关联的任务信息, 产品id: {}", productInfo.getId());
            throw new ServiceException("该产品没有关联的任务信息");
        }
        // mes 不需要调用该接口关闭告警
        //alarmRecordService.stopAlarm(task, task.getMaterialCode(), AlarmTypeEnum.QUALITY_EXCEPTION, String.valueOf(productInfo.getId()));

        productInfo.setIsQualified(true);
        productInfo.setHandleRemark(handleDTO.getRemark());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        productInfo.setHandleUserCode(loginUser.getUsername());
        productInfo.setHandleUserName(loginUser.getUser().getNickName());
        return productInfoService.updateById(productInfo);
    }
}
